package com.example.pplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;

public class TaskDetailActivity extends AppCompatActivity {
    private TextView tvTitle, tvDescription, tvPriority, tvDueDate, tvCategory, tvReminder;
    private Button btnComplete;
    private TaskManager taskManager;
    private long taskId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 深色模式适配
        SharedPreferences prefs = getSharedPreferences("user_prefs", MODE_PRIVATE);
        boolean darkMode = prefs.getBoolean("dark_mode", false);
        AppCompatDelegate.setDefaultNightMode(
                darkMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
        );

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_detail);

        taskId = getIntent().getLongExtra("task_id", -1);
        long currentUserId = getCurrentUserId();
        taskManager = new TaskManager(this, currentUserId);

        // 初始化视图
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        tvTitle = findViewById(R.id.tv_title);
        tvDescription = findViewById(R.id.tv_description);
        tvPriority = findViewById(R.id.tv_priority);
        tvDueDate = findViewById(R.id.tv_due_date);
        tvCategory = findViewById(R.id.tv_category);
        tvReminder = findViewById(R.id.tv_reminder);
        btnComplete = findViewById(R.id.btn_complete);

        // 加载任务详情
        loadTaskDetails();

        // 完成按钮点击事件
        btnComplete.setOnClickListener(v -> completeTask());
    }

    private void loadTaskDetails() {
        Cursor cursor = taskManager.getTaskDetails(taskId);
        if (cursor != null && cursor.moveToFirst()) {
            tvTitle.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_TITLE)));
            tvDescription.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_DESCRIPTION)));
            tvPriority.setText("优先级: " + cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_PRIORITY)));
            tvDueDate.setText("截止日期: " + cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_DUE_DATE)));
            tvCategory.setText("分类: " + cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_CATEGORY)));
            tvReminder.setText("提醒时间: " + cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COLUMN_REMINDER_TIME)));
            cursor.close();
        }
    }

    private void completeTask() {
        if (taskManager.markTaskCompleted(taskId)) {
            Toast.makeText(this, "任务已完成", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        } else {
            Toast.makeText(this, "标记任务完成失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteTask() {
        if (taskManager.deleteTask(taskId)) {
            Toast.makeText(this, "任务已删除", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        } else {
            Toast.makeText(this, "删除任务失败", Toast.LENGTH_SHORT).show();
        }
    }

    private long getCurrentUserId() {
        SharedPreferences prefs = getSharedPreferences("user_prefs", MODE_PRIVATE);
        return prefs.getLong("current_user_id", -1);
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        getMenuInflater().inflate(R.menu.menu_task_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull android.view.MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        } else if (id == R.id.action_edit) {
            editTask();
            return true;
        } else if (id == R.id.action_delete) {
            deleteTask();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void editTask() {
        Intent editIntent = new Intent(this, EditTaskActivity.class);
        editIntent.putExtra("task_id", taskId);
        startActivityForResult(editIntent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            loadTaskDetails();
            setResult(RESULT_OK);
        }
    }
}